package com.example.spirngbatch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableBatchProcessing //启用springbatch框架 需要的注解
public class SpirngbatchApplication {

    // job调度器
    @Autowired
    private JobLauncher jobLauncher;

    // job构建工厂
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // step步骤构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    // 任务step执行逻辑由 tasklet完成
    @Bean
    public Tasklet tasklet1() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello SpringBatch !");

                // 返回执行任务完成
                return RepeatStatus.FINISHED;
            }
        };
    }

    // 作业步骤 不带不读写操作
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").tasklet(tasklet1()).build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("hello-job").start(step1()).build();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpirngbatchApplication.class, args);
    }

}
